package com.pur.zsms.ordercustommanage.service.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import net.sf.cglib.beans.BeanCopier;

import org.springframework.stereotype.Service;

import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.matermanage.mater.dao.IMaterDao;
import com.pur.zsms.matermanage.mater.po.MaterPo;
import com.pur.zsms.matermanage.mater.po.UnitMaterConvertPo;
import com.pur.zsms.ordercustommanage.dao.ISelectFoodDao;
import com.pur.zsms.ordercustommanage.po.OrderCustFoodPo;
import com.pur.zsms.ordercustommanage.po.OrderSupplierMaterPo;
import com.pur.zsms.ordercustommanage.po.OrderSupplierMaterRealPo;
import com.pur.zsms.ordercustommanage.po.OrderSupplierPo;
import com.pur.zsms.ordercustommanage.service.ICustMaterOrderService;
import com.pur.zsms.ordercustommanage.vo.OrderSupplierVo;
import com.pur.zsms.supplier.dao.SupplierOrderDao;

@Service("custMaterOrderService")
public class CustMaterOrderServiceImpl implements ICustMaterOrderService {
	@Resource(name = "supplierOrderDao")
	private SupplierOrderDao supplierOrderDao;

	@Resource(name = "selectFoodDao")
	private ISelectFoodDao selectFoodDaoImpl;

	@Resource(name = "materDao")
	private IMaterDao materDaoImpl;

	public ISelectFoodDao getSelectFoodDaoImpl() {
		return selectFoodDaoImpl;
	}

	public void setSelectFoodDaoImpl(ISelectFoodDao selectFoodDaoImpl) {
		this.selectFoodDaoImpl = selectFoodDaoImpl;
	}

	public SupplierOrderDao getSupplierOrderDao() {
		return supplierOrderDao;
	}

	public void setSupplierOrderDao(SupplierOrderDao supplierOrderDao) {
		this.supplierOrderDao = supplierOrderDao;
	}

	@Override
	public PaginationSupport listSupplierOrder(PageController pageController, OrderSupplierVo orderSupplierVo) throws GenericException {
		return supplierOrderDao.listSupplierOrder(pageController, orderSupplierVo);
	}

	@Override
	public PaginationSupport listCustOrder(PageController pageController, OrderSupplierVo orderSupplierVo) throws GenericException {
		return supplierOrderDao.listCustOrder(pageController, orderSupplierVo);
	}

	@Override
	public PaginationSupport listCustOrderAllForpurchase(PageController pageController, OrderSupplierVo orderSupplierVo) throws GenericException {
		return supplierOrderDao.listCustOrderAllForpurchase(pageController, orderSupplierVo);
	}

	@Override
	public OrderSupplierPo getOrderSup(String ordId, String ordCustId, String ordSupId) throws GenericException {
		return supplierOrderDao.getOrderSup(ordId, ordCustId, ordSupId);
	}

	@Override
	public void sendmater(OrderSupplierPo orderSupplierPo) throws GenericException {
		orderSupplierPo.setOrdState(5);// 收货完毕
		orderSupplierPo.setOrdPaymentState(2);// 待结款
		supplierOrderDao.updateOrderSupplierPo(orderSupplierPo);
	}

	public void deleteOrderSupperMater(String ordId, String ordMaterId, String ordSupId) throws GenericException {
		supplierOrderDao.deleteOrderSupperMater(ordId, ordMaterId, ordSupId);
	}

	public void updateOrderSupperMater(String ordId, String ordMaterId, String ordSupId, String ordCustId) throws GenericException {
		// 设置供货商订单
		OrderSupplierMaterPo tOrderSupplierMaterPo = supplierOrderDao.getOrderSupMaterByOrdIdMaterId(ordId, ordMaterId);
		// 获取原来供货商supid
		int supIdArche = tOrderSupplierMaterPo.getOrdSupId();

		tOrderSupplierMaterPo.setOrdSupId(Integer.valueOf(ordSupId));
		selectFoodDaoImpl.updateOrderSupplierMater(tOrderSupplierMaterPo);
		// 设置实际订单
		OrderSupplierMaterRealPo tOrderSupplierMaterRealPo = supplierOrderDao.getOrderSupMaterRealByOrdIdMaterId(Integer.valueOf(ordId),
				Integer.valueOf(ordMaterId));
		tOrderSupplierMaterRealPo.setOrdSupId(Integer.valueOf(ordSupId));
		selectFoodDaoImpl.updateOrderSupplierMaterReal(tOrderSupplierMaterRealPo);
		// 设置供货商订单总表
		List<OrderSupplierPo> orderSupplierPoList = supplierOrderDao.getOrderSupList(ordId, ordCustId, ordSupId);

		OrderSupplierPo orderSupplierPo = supplierOrderDao.getOrderSup(ordId, ordCustId, String.valueOf(supIdArche));
		// 新供货商的价格汇总
		BigDecimal valueReal = supplierOrderDao.computeValue(Integer.valueOf(ordId), Integer.valueOf(ordSupId));
		// 如果新供货商没有其他货物
		if (orderSupplierPoList == null || orderSupplierPoList.size() == 0) {
			OrderSupplierPo tOrderSupplierPo = new OrderSupplierPo();
			BeanCopier copier = BeanCopier.create(OrderSupplierPo.class, OrderSupplierPo.class, false);
			copier.copy(orderSupplierPo, tOrderSupplierPo, null);
			tOrderSupplierPo.setOid(0);
			tOrderSupplierPo.setOrdSupId(Integer.valueOf(ordSupId));

			tOrderSupplierPo.setOrdValue(valueReal);
			selectFoodDaoImpl.updateOrderSupplier(tOrderSupplierPo);
		} else {
			OrderSupplierPo tOrderSupplierPo = orderSupplierPoList.get(0);
			tOrderSupplierPo.setOrdValue(valueReal);
			selectFoodDaoImpl.updateOrderSupplier(tOrderSupplierPo);
		}
		// 删除老供货商实体表
		List<OrderSupplierMaterPo> tOrderSupplierMaterPoDeletelist = supplierOrderDao.getOrderSupMaterByOrdIdSupId(ordId, String.valueOf(supIdArche),
				ordMaterId);

		if (tOrderSupplierMaterPoDeletelist == null || tOrderSupplierMaterPoDeletelist.size() == 0) {
			selectFoodDaoImpl.deleteSupOrderByOidSupId(Integer.valueOf(ordId), supIdArche);
		} else {
			// 有则改变价格
			List<OrderSupplierPo> orderSupplierPoListArche = supplierOrderDao.getOrderSupList(ordId, ordCustId, String.valueOf(supIdArche));
			// 老供货商的价格汇总
			BigDecimal valueRealArche = supplierOrderDao.computeValue(Integer.valueOf(ordId), Integer.valueOf(supIdArche));
			OrderSupplierPo tOrderSupplierPoArche = orderSupplierPoListArche.get(0);
			tOrderSupplierPoArche.setOrdValue(valueRealArche);
			selectFoodDaoImpl.updateOrderSupplier(tOrderSupplierPoArche);
		}
	}

	public boolean checkPassOrderValueForPurchase(long ordId, long supId) throws GenericException {

		List<OrderSupplierPo> ordsupplierList = supplierOrderDao.getOrderSupListByOrdIdAndSupId(ordId, supId);

		OrderSupplierPo tOrderSupplierPo = ordsupplierList.get(0);
		if (tOrderSupplierPo.getOrdState() == 5) {
			return false;
		}
		return true;
	}

	public void unitMaterChange(int ordId, int materId, int auxiliaryMaterId, int supId, int ordCustId, BigDecimal ordMaterWeightReal, int materLevel)
			throws GenericException {

		OrderCustFoodPo tOrderCustFood = selectFoodDaoImpl.getOrderCustFoodByOrdIdMaterIdMaterLevel(ordId, auxiliaryMaterId, materLevel).get(0);
		tOrderCustFood.setOrdMaterId(materId);
		// tOrderCustFood.setOrdMaterNum(ordMaterWeightReal);
		selectFoodDaoImpl.updateOrderCustFoodPo(tOrderCustFood);

		OrderSupplierMaterPo tOrderSupplierMaterPo = supplierOrderDao.getOrderSupMaterByOrdIdMaterId(String.valueOf(ordId),
				String.valueOf(auxiliaryMaterId));
		tOrderSupplierMaterPo.setOrdMaterId(materId);
		tOrderSupplierMaterPo.setOrdMaterValue(new BigDecimal(0));
		// tOrderSupplierMaterPo.setOrdMaterWeight(ordMaterWeightReal);
		selectFoodDaoImpl.updateOrderSupplierMater(tOrderSupplierMaterPo);

		OrderSupplierMaterRealPo tOrderSupplierMaterRealPo = supplierOrderDao.getOrderSupMaterRealByOrdIdMaterId(ordId, auxiliaryMaterId);
		tOrderSupplierMaterRealPo.setOrdMaterId(materId);
		tOrderSupplierMaterRealPo.setOrdMaterValue(new BigDecimal(0));
		tOrderSupplierMaterRealPo.setOrdMaterValueCost("0");
		tOrderSupplierMaterRealPo.setOrdMaterValueCostUnit("0");
		tOrderSupplierMaterRealPo.setOrdMaterValueUnit(new BigDecimal(0));
		tOrderSupplierMaterRealPo.setOrdMaterWeight(ordMaterWeightReal);
		selectFoodDaoImpl.updateOrderSupplierMaterReal(tOrderSupplierMaterRealPo);

	}

	public void unitMaterChangeBatch(int ordId, int supId) throws GenericException {
		// 取得需要转换的单位
		List<OrderCustFoodPo> tOrderCustFoodList = selectFoodDaoImpl.getOrderCustFoodByOrdIdForAuxiliaryMater(ordId, supId);
		if (tOrderCustFoodList != null && tOrderCustFoodList.size() > 0) {
			for (OrderCustFoodPo tOrderCustFood : tOrderCustFoodList) {
				UnitMaterConvertPo tUnitMaterConvertPo = materDaoImpl.getUnitMaterConvertPo(supId, tOrderCustFood.getOrdMaterId()).get(0);
				MaterPo materPoHost = materDaoImpl.getMaterPoByMaterId(tUnitMaterConvertPo.getHostMaterId()).get(0);
				MaterPo materPoAuxiliary = materDaoImpl.getMaterPoByMaterId(tUnitMaterConvertPo.getAuxiliaryMaterId()).get(0);

				tOrderCustFood.setOrdMaterId(materPoHost.getMaterId());
				// tOrderCustFood.setOrdMaterNum(new BigDecimal(0));
				selectFoodDaoImpl.updateOrderCustFoodPo(tOrderCustFood);

				OrderSupplierMaterPo tOrderSupplierMaterPo = supplierOrderDao.getOrderSupMaterByOrdIdMaterId(String.valueOf(ordId),
						String.valueOf(materPoAuxiliary.getMaterId()));
				tOrderSupplierMaterPo.setOrdMaterId(materPoHost.getMaterId());
				tOrderSupplierMaterPo.setOrdMaterValue(new BigDecimal(0));
				tOrderSupplierMaterPo.setLeaveMessage(tOrderSupplierMaterPo.getOrdMaterWeight() + materPoAuxiliary.getUnit());
				// tOrderSupplierMaterPo.setOrdMaterWeight(ordMaterWeightReal);
				selectFoodDaoImpl.updateOrderSupplierMater(tOrderSupplierMaterPo);

				OrderSupplierMaterRealPo tOrderSupplierMaterRealPo = supplierOrderDao.getOrderSupMaterRealByOrdIdMaterId(ordId,
						materPoAuxiliary.getMaterId());
				tOrderSupplierMaterRealPo.setOrdMaterId(materPoHost.getMaterId());
				tOrderSupplierMaterRealPo.setOrdMaterValue(new BigDecimal(0));
				tOrderSupplierMaterRealPo.setOrdMaterValueCost("0");
				tOrderSupplierMaterRealPo.setOrdMaterValueCostUnit("0");
				tOrderSupplierMaterRealPo.setOrdMaterValueUnit(new BigDecimal(0));
				tOrderSupplierMaterRealPo.setOrdMaterWeight(new BigDecimal(0));
				selectFoodDaoImpl.updateOrderSupplierMaterReal(tOrderSupplierMaterRealPo);
			}
		}

	}

}
